;(function($){
	$.fn.balance_transfer = function(opt){
		var el = this;
		var opts = {
			no : "",
			dataBack : function(){},
			closeBack : function(){},
		}
		var param = $.extend(opts,opt);
		var app = new App(el,param);
	}
	
	var App = function(el,param){
		this.el = el;
		this.param = param;
		this.initHtml();
		this.initGrid();
		this.getBmDs();
		this.bindEevnt();
	}
	
	App.prototype = {
		bindEevnt : function(){
			var el = this.el;
			var param = this.param;
			var t = this;
			
			//取消按钮
			el.find("#modalBtnCancel").click(function(){
				el.modal("hide");
				el.empty();
				param.closeBack();
			})
			
			//查询按钮
			el.find("#modalBtnSearch").click(function(){
//				t.getDs();
				var grid_selector = el.find("#tableGrid");
				grid_selector.jqGrid("GridUnload");
				t.initGrid();
			})
			
			//选中按钮
			el.find("#modalBtnSel").click(function(){
				t.wait();
				t.balanceCheckout();
			})
		},
		initHtml :function(){
			var el = this.el;
			var param = this.param;
			var t = this;
			
    		this.el.addClass("modal").attr("tabindex","-2").attr("data-backdrop","static");
    		var html='<div class="modal-dialog" style="width:800px;">'+
			    		'<div id="pageOverlay" class="hide"></div>'+
						'<div id="loading" class="loading hide">'+
							'<i class="ace-icon fa fa-spinner fa-spin orange bigger-250"></i>'+
						'</div>'+
						'<div class="modal-content">'+     
				  			'<div class="modal-header no-padding">'+
								'<div class="table-header widget-header-large " style="text-align:center; font-size:20px">'+  
									'<span>'+ $.i18n.propJsp("余额转账") +'</span>'+       
								'</div>'+
							'</div>'+
							'<div class="modal-body">'+
								'<div class="col-sm-6" style="border:1px solid #ffc500fa;padding:5px;padding-left:10px!important;">'+
									'<form id="frm" class="form-horizontal">'+
										'<div class="form-group">'+
											'<label class="col-sm-3 control-label">'+$.i18n.propJsp("营业部门号")+'</label>'+
											'<div class="col-sm-9">'+
												'<span class="input-icon input-icon-right">'+
													'<select class="form-control bmSelect" id="bm" name="bm" style="width:200px;"></select>'+
												'</span>'+
											'</div>'+
										'</div>'+
										'<div class="form-group">'+
											'<label class="col-sm-3 control-label">'+$.i18n.propJsp("台号(手牌号)")+'</label>'+
											'<div class="col-sm-9">'+
												'<span class="input-icon input-icon-right">'+
													'<select class="form-control thSelect" id="th" name="th" style="width:200px;"></select>'+
												'</span>'+
											'</div>'+
										'</div>'+
									'</form>'+
								'</div>'+
								'<div class="col-sm-6" style="border:1px solid #ffc500fa;padding:5px;padding-left:10px!important;">'+
									'<div class="clearfix">'+
										'<div class="pull-right" style="margin-top:19px;margin-bottom:19px;">'+
											'<div class="btn-group">'+
												'<button id="modalBtnSearch" class="btn btn-large btn-info">'+
													$.i18n.propJsp("查询")+
												'</button>'+
											'</div>'+
											'<div class="btn-group">'+
												'<button id="modalBtnSel" class="btn btn-large btn-info">'+
													$.i18n.propJsp("选中")+
												'</button>'+
											'</div>'+
											'<div class="btn-group">'+
												'<button id="modalBtnCancel" class="btn btn-large btn-default">'+
													$.i18n.propJsp("取消")+
												'</button>'+
											'</div>'+
										'</div>'+
									'</div>'+
								'</div>'+
								'<div class="col-sm-12 widget-box">'+
									'<div id="gridWidth" class="widget-body" style="height:397px;">'+
										'<table id="tableGrid"></table>'+
										'<div id="gridPage"></div>'+
									'</div>'+
								'</div>'+
							'</div>'+		
		  				'</div>'+
		  			'</div>'+		
				'</div>'+
			'</div>';
    		el.html(html);
		},
		initGrid : function(){
			var el = this.el;
			var grid_selector = this.el.find("#tableGrid");
			var pager_selector = this.el.find("#gridPage");
			var arg = {"arg":{"arg":"加菜"},"where":{"bmh":(el.find("#bm").val()||""),"cwlx":"","all_search":""}}
			var arg1 = {"page":1,"sord":"asc","rows":-1,"sidx":"sort,bh"}
			grid_selector.jqGrid({
				url : $.HSAPI_AJAXURL+"/qms/mobile/procedure2",  
				datatype : "json",
//				datatype : "local",
//				data : [],
				width :766,
				height : 300,
				postData : $.HSAPI_ASKJSONINFO( JSON.stringify(arg),'POSRetrieveTableView',JSON.stringify(arg1),"list"),
				colNames : ["ID","台号","名称","人数","消费金额","状态","no"],
				colModel : [{
					name : "id",//"ID",
					index : "id",
					width: 50,
					hidden :true,
				},{
					name : "bh",//"台号",
					index : "bh",
					width: 60,
				},{
					name : "mc",//"名称",
					index : "mc",
					width: 100,
				},{
					name : "rs",//"人数",
					index : "rs",
					width: 60,
					align : "right",
				},{
					name : "xfje",//"消费金额",
					index : "xfje",
					width: 60,
					align : "right",
					formatter : "number",
				},{
					name : "status",//"状态",
					index : "status",
					width: 50,
				},{
					name : "no",//"no"
					index : "no",
					width: 60,
					hidden : true,
				}],    	
				scroll : true,
				sortname : "id",
				sortorder : "asc",
				viewrecords : true,
				rowNum : "全部",
//				rowList:[10,20,30],
				emptyrecords: $.i18n.propJsp("没有搜索到任何数据"),
				recordtext: $.i18n.propJsp("共计")+":{2}"+$.i18n.propJsp("条"),
				pager : pager_selector,
				altRows : true, 
				scrollrows:true,
		        ondblClickRow: function(rowid){  
		        	
		        },
		        onSelectRow:function(rowid){
		        
		        },
				loadComplete : function(data){
					var table = this;
					setTimeout(function(){
//						updatePagerIcons(table);
//						enableTooltips(table);
					}, 0);
					
					var ids = grid_selector.jqGrid("getGridParam","selarrrow");
					grid_selector.jqGrid("setSelection",ids[0]);
				}, 
			});
		},
		getDs : function(){
			var el = this.el;
			var param = this.param;
			var t = this;
			var json = {"where":{"bmh":(el.find("#bm").val()||""),"th":(el.find("#th").val()||"")}}
			jQuery.ajax({  
				type : 'POST',
				url : $.HSAPI_AJAXURL+"/qms/mobile/procedure2",  
				data : $.HSAPI_ASKJSONINFO(JSON.stringify(json),'POSRetrieveReservationView',{}),
				dataType : 'json',  
				success : function(data){
					if(data.RETURN==1){
						console.log(JSON.stringify(data.th))
						el.find("#tableGrid").jqGrid("clearGridData");
						$(data.th).each(function(k,v){
							el.find("#tableGrid").jqGrid('addRowData',(k+1),v);
						})
					}else{
//						jf_alert(data.MSG)
					}
				},  
				error : function(data){ 
					jf_alert(JSON.stringify(data))
				}  
			});
		},
		getBmDs : function(){
			var t = this;
			var el = this.el;
			var param = this.param;
			
			var where = {};
        	var fileds={};
        	var wherestring = {}
        	fileds = {"fileds":{"fileds":["bh","mc"]}};
        	var str=" bh != '000' and bh != '999'"
        	wherestring = {"wherestring":["@@WS",str]}
        	where = {where:{1:1}}
        	var table={};
        	table.table={"table":"bm"};				
        	c= jQuery.extend({}, where,fileds,table,wherestring);
        	jQuery.ajax({  
        		type : 'POST',
//        		async:false, 
        		url :  $.HSAPI_AJAXURL+"/qms/mobile/procedure2",  
        		data : $.HSAPI_ASKJSONINFO( JSON.stringify(c),'ExecRetrieveJson','{}'),
        		dataType : 'json',  
        		success : function(data){
                    if(data.RETURN == 1){
                    	el.find(".bmSelect").empty();
                    	el.find(".bmSelect").append('<option class="bm_item" value=""></option>')
                    	$(data.DATA).each(function(k,v){
                    		el.find(".bmSelect").append('<option class="bm_item" value="'+v.bh+'">'+v.mc+'</option>')
                    	})
//                    	t.getDs();
                    }else{
                    	jf_alert(JSON.stringify(data));
                    }
        		},  
        		error : function(data){
        			jf_alert(JSON.stringify(data));
        		}  
        	});
		},
		balanceCheckout : function(){
			var el = this.el;
			var param = this.param;
			var t = this;
			
			var grid_selector = el.find("#tableGrid");
			var rowid = grid_selector.jqGrid('getGridParam','selrow')
			if(!rowid){
				jf_alert("请选择要转账的台号！！！")
				return false;
			}
			var rowdata = $(grid_selector).jqGrid("getRowData",rowid);
			
			var param1={},param2={},param3="",param4=param.no
			var noout="no"
			param1={"arg":{"data":"no","no":param.no,"no_to":(rowdata.no||""),"noout":noout}}
			jQuery.ajax({  
	    		type : 'POST',
	    		async:false, 
	    		url :  $.HSAPI_AJAXURL+"/pos/front/hoguest/procedure4",    
	    		data : $.HSAPI_ASKJSONINFO( JSON.stringify(param1),'POSOrderTransfer_Detail',JSON.stringify(param2),param3,param4),
	    		dataType : 'json',  
	    		success : function(data){
	    			t.goon();
	                if(data.RETURN == 1){
	                	jf_success("余额转账成功！")
	                	el.empty();
	                	el.modal("hide");
	                	param.dataBack(data);
	                }else if(data.RETURN == 10){
	                	if(!$.isFunction($().alertModal)){
	    					$.ajaxSetup({ async: false, cache: false });
	    		            $.getScript("js/cashier_mana/jquery.alertModal.js");
	    		            $.ajaxSetup({ async: true });
	    				}
	                	$("").alertModal({
    						data:data,
    						getDefaultBack:function(json){
    							param.dataBack();
    						}
    					})
	                }else{
	                	jf_alert(data.MSG)
	                }
	    		},  
	    		error : function(data){
	    			t.goon();
	    			jf_alert(JSON.stringify(data))
	    		}  
	    	});
		},
		goon : function(){
			var el = this.el;
			el.find("#loading").addClass("hide");
			el.find("#pageOverlay").addClass("hide");
		},
		wait : function(){
			var el = this.el;
			el.find("#loading").removeClass("hide");
			el.find("#pageOverlay").removeClass("hide");
		}
	}
	
})(jQuery);